package com.zl.service.base;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.ResDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.code.GlobalDictEnum;
import com.zl.common.enums.resp.IRespEnum;
import com.zl.common.utils.PageUtil;
import com.zl.common.utils.ReturnUtil;
import com.zl.entity.base.BaseTime;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ReflectUtil;

/**
 * 某些普遍方法再次封装，减少引入快速使用
 * @author zhouliang
 *
 */
public class BaseManager {

    protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
    
    
    /** 获取时间*/
    public BaseTime getSystemTime() {
        BaseTime timeVO = new BaseTime();
        timeVO.setUtcTime(Instant.now());
        timeVO.setDate(Date.from(timeVO.getUtcTime()));
        timeVO.setDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withLocale(Locale.SIMPLIFIED_CHINESE).withZone(ZoneId.of(timeVO.getTimeZone())).format(timeVO.getUtcTime()));
        timeVO.setDateDay(timeVO.getDateTime().substring(0, 10));
        return timeVO;
    }
    
    /** 
     * 处理分页属性，校验正确性
     * @return 
     */
    public static PageReqVO pageHandle(PageReqVO page) {
        if(page==null){
            page = new PageReqVO();
            page.setPage(1);
            return page;
        }
        if(page.getLimit()==0 && page.getPage()==0){
            page.setPage(1);
        }
        PageUtil.pageHandle(page);
        return page;
    }
    
    /**
     * 返回成功
     */
    public static <T> ResDTO<T> successDATA(T data){
        return ReturnUtil.successDATA(data);
    }
    /**
     * 返回失败
     */
    public static <T> ResDTO<T> errorDATA(T data){
        return ReturnUtil.errorDATA(data);
    }
    /**
     * 自定返回
     */
    public static <T> ResDTO<T> returnDATA(IRespEnum code,T data){
        return ReturnUtil.returnDATA(code,data);
    }
    
    
    /** 更新关键字段*/
    public <T> void initUpdateInfo(T info, ReqDTO<?> reqInfo, Date time) {
        try{
            addDefaultUserInfo(reqInfo);
            ReflectUtil.setFieldValue(info, "lastUpdName", reqInfo.getReqUserName());
            ReflectUtil.setFieldValue(info, "lastUpdOprid", reqInfo.getReqUserId());
            ReflectUtil.setFieldValue(info, "lastUpdTm", time);
            if(ReflectUtil.hasField(info.getClass(), "randomToken")){
                ReflectUtil.setFieldValue(info, "randomToken", IdUtil.simpleUUID().substring(0, 10));
            }
        }catch(Exception e){
        }
    }
    
    /** 创建关键字段*/
    public <T> void initCreateInfo(T info, ReqDTO<?> reqInfo, Date time) {
        try{
            addDefaultUserInfo(reqInfo);
            ReflectUtil.setFieldValue(info, "createName", reqInfo.getReqUserName());
            ReflectUtil.setFieldValue(info, "createOprid", reqInfo.getReqUserId());
            ReflectUtil.setFieldValue(info, "createTm", time);
            if(ReflectUtil.hasField(info.getClass(), "dataStatus")){
                ReflectUtil.setFieldValue(info, "dataStatus", GlobalDictEnum.DATA_STATUS.ENABLE.getCode()); 
            }
            initUpdateInfo(info, reqInfo, time);
        }catch(Exception e){
        }
    }
    
    /**临时默认用户*/
    protected void addDefaultUserInfo(ReqDTO<?> reqInfo){
        if(StringUtils.isBlank(reqInfo.getReqUserId())){
            ReflectUtil.setFieldValue(reqInfo, "reqUserId", "9999999999");
            ReflectUtil.setFieldValue(reqInfo, "reqUserName", "zhoul");
        }
    }
 }